﻿using System;
using System.Net;
using System.ComponentModel;

namespace VPNLaunch
{
  public class Route
  {
    [Category("Routes")]
    [Description("Network Mask for route (i.e. 255.55.255.0")]
    public String NetworkMask { get; set; }

    [Category("Routes")]
    [Description("Network Address for route (i.e. 172.17.0.10")]
    public String NetworkAddress { get; set; }
    
    [Category("Routes")]
    [Description("Gateway for route (i.e. 172.17.0.1")]
    [Browsable(false)]
    public String Gateway { get; set; }

    
    public Route() { }

    public Route(string NetworkAddress,
                 string NetworkMask,
                 string Gateway)
    {
      if (!IsIpValid(NetworkAddress))
        throw new ArgumentException("Network Address is invalid");
      if (!IsIpValid(NetworkMask))
        throw new ArgumentException("Network Mask is invalid");
      if (!IsIpValid(Gateway))
        throw new ArgumentException("Gateway address is invalid");

      this.NetworkAddress = NetworkAddress;
      this.NetworkMask = NetworkMask;
      this.Gateway = Gateway;
    }

    public override string ToString()
    {
      return String.Format("{0}/{1}", NetworkAddress, NetworkMask);
    }
    /// <summary>
    /// Verifies IP Address is Valid
    /// </summary>
    private bool IsIpValid(string IPAddress)
    {
      string[] token = IPAddress.Split('.');
      if (token.Length != 4) return false;
      byte fred = 0;
      foreach (string octet in token)
      {
        if (!(byte.TryParse(octet, out fred)))
          return false;
      }
      return true;
    }
  }
}